from werkzeug.exceptions import HTTPException

from apps.libs.error import APIException
from apps.libs.error_code import ServerError

from exts import app

# 捕捉所有类型的异常
@app.errorhandler(Exception)
def framework_error(e):
    if isinstance(e, APIException):
        return e
    if isinstance(e, HTTPException):
        code = e.code
        msg = e.description
        error_code = 1007
        return APIException(code=code, error_code=error_code, msg=msg)
    else:
        # 不确定的异常处理
        if not app.config['DEBUG']:
            return ServerError()
        else:
            return e

if __name__ == '__main__':
    app.run(debug=app.config['DEBUG'], port=5001, host='0.0.0.0')